package com.xcoder.leetcode;

/**
 * https://leetcode-cn.com/problems/merge-sorted-array/
 */
public class _88_合并两个有序数组 {

    public static void main(String[] args) {
//        int a[] = {1, 2, 3, 0, 0, 0};
//        int b[] = {2, 5, 6};
//        merge(a, 3, b, 3);

//        int a[] = {1};
//        int b[] = {0};
//        merge(a, 1, b, 0);

//        int a[] = {0};
//        int b[] = {1};
//        merge(a, 0, b, 1);

        int a[] = {2, 0};
        int b[] = {1};
        merge(a, 1, b, 1);

        for (int i : a) {
            System.out.print(i + " ");
        }
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = m - 1;
        int j = n - 1;
        int k = m + n - 1;
        while (i >= 0 && j >= 0) {
            if (nums1[i] > nums2[j]) {
                nums1[k--] = nums1[i--];
            } else {
                nums1[k--] = nums2[j--];
            }
        }
        while (j >= 0) {
            nums1[k--] = nums2[j--];
        }
    }

}
